import { ref } from 'vue'
import { EvaluateItemType, EvaluateType } from '../utils/types'
import { UserTypesEm } from '@/store/enums'
import { EvaluteTypeEm } from './enums'
import store from '@/store'
import defaultAvatar from '@/assets/image/introduce/default_avatar.png'
const useEvaluate = (userId: string, userType: string, router: any): any => {
  // 评价列表
  const evaluateList = ref<EvaluateItemType[]>([])

  // 构建评价列表数据
  const buildEvaluateData = (data: EvaluateType[]): any => {
    const list = data?.slice(0, 3) ?? []
    let name = ''
    if (UserTypesEm.CARGOOWNER.value === Number(userType)) {
      name = 'driverName'
    } else if (UserTypesEm.DRIVER.value === Number(userType)) {
      name = 'consName'
    }
    list.forEach(item => {
      evaluateList.value.push({
        name: item[name],
        time: item.commentTime,
        startNumber: item.starNum,
        memo: item.content,
        avatar: defaultAvatar
      })
    })
  }

  // 获取评价
  const handleGetEvaluate = async (type?: number): Promise<void> => {
    const params: any = {
      pageNo: 1,
      pageSize: 10
    }
    if (UserTypesEm.CARGOOWNER.value === Number(userType)) {
      params.consId = userId
      params.type = type ? type : EvaluteTypeEm.DRIVER_TO_CONS.value
    } else if (UserTypesEm.DRIVER.value === Number(userType)) {
      params.driverId = userId
      params.type = type ? type : EvaluteTypeEm.CONS_TO_DRIVER.value
    }
    const result = await store.dispatch('introduce/handleTransportBillCommentPage', params)
    buildEvaluateData(result?.rows ?? [])
  }

  // 点击去更多评价页面
  const onMoreEvalute = (): void => {
    router.push({
      name: 'EvaluateDetail',
      query: {
        userId,
        userType
      }
    })
  }

  return {
    evaluateList,
    handleGetEvaluate,
    onMoreEvalute
  }
}

export default useEvaluate
